﻿using System;
using Microsoft.BizTalk.Component.Interop;
using log4net;
using AppFx.BizTalk.Diagnostics;

namespace AppFx.BizTalk.PipelineComponents
{
    /// <summary>
    /// Helper class for interacting with property bad
    /// </summary>
    public sealed class PropertyBagHelper
    {
        private static readonly ILog Log = LogFactory.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Returns a cast property
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static object Read(IPropertyBag pb, string propertyName)
        {
            if (pb == null)
                throw new ArgumentNullException("pb");

            object val = null;

            try
            {
                pb.Read(propertyName, out val, 0);
                return val;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }
        /// <summary>
        /// Returns a cast property
        /// </summary>
        /// <param name="pb"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        public static void Write(IPropertyBag pb, string propertyName, object propertyValue)
        {
            if (pb == null)
            {
                Log.Error("Pb is null", null);
                throw new ArgumentNullException("pb");
            }

            pb.Write(propertyName, ref propertyValue);
        }
    }
}

